/**
 * State管理
 * 这个State是基于Localstorage开发,可以直接存入json,array,string,同样也可以直接原类型取出
 */
module.exports = function(Mogo) {
  Mogo.State = {
    // 设置Sate
    set(k, v) {
      let state = v;
      //判断参数
      if (typeof k !== "string") throw "key must be a string";
      if (v === null) wx.removeStorageSync(k);
      //判断类型
      if (typeof v !== "string") state = JSON.stringify(state);
      state = wx.setStorageSync(k, state);
      return state;
    },
    // 获取state
    get(k) {
      if (typeof k !== "string") throw "key must be a string";
      var state = wx.getStorageSync(k);
      if (state === null) return null;
      // 用try来判定存的是不是数组或对象
      try {
        return JSON.parse(state);
      } catch (e) {
        return state;
      }
    }
  };
};
